package online.flowerinsnow.tcpudpclient.scheduler

import java.util.concurrent.Callable
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.ScheduledThreadPoolExecutor
import java.util.concurrent.TimeUnit

class Scheduler {
    companion object {
        private val EXECUTOR_INSTANCE = ScheduledThreadPoolExecutor(32)
        fun <T> runAsync(callable : Callable<T>) : ScheduledFuture<T> {
            return schedule(callable, 0L)
        }

        fun <T> schedule(callable : Callable<T>, delay : Long) : ScheduledFuture<T> {
            return EXECUTOR_INSTANCE.schedule(callable, delay, TimeUnit.MILLISECONDS)
        }

        fun schedule(runnable : Runnable, delay : Long, period : Long) : ScheduledFuture<*> {
            return EXECUTOR_INSTANCE.scheduleWithFixedDelay(runnable, delay, period, TimeUnit.MILLISECONDS)
        }
    }
}